package com.base.cn.platform.os.entity.exam.record;

import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.entity.exam.question.ExamQuestion;
import com.base.cn.platform.os.entity.exam.template.TemplateContent;
import com.google.gson.Gson;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户分数对象
 *
 * @author s.li
 * @date 2018/12/11
 */
@Data
@Slf4j
public class AnswerJson {
    /**1无子试题，2有子试题*/
    private int type;
    /**试题类型*/
    private int typeValue;
    /**选项题答案列表*/
    private List<String> answerOptList;
    /**选项题，是否答对，true对，false错*/
    private boolean optAnswer;
    /**填空题答案列表*/
    private List<String> answerBlankList;
    /**填空题，用户答案对错*/
    private List<Map<String,Object>> blankAnswerMapList;
    /**简答题答案*/
    private String specialAnswer;
    /**用户的答案*/
    private String userSpecialAnswer;
    /**简答题是否答对，true，false*/
    private boolean sAnswer;
    /**简答题答案图片List*/
    private List<String> specialImgList;
    /**用户得分*/
    private int userScore;
    /**评语*/
    private String comment;
    /**子试题答案列表*/
    private List<AnswerJson> subAnswerList;
    /**小试题下标，用于复合试题作答时提交的下标*/
    private int index;

    /**
     * 格式化
     * @param answerJsonContext 数据内容
     * @return AnswerJson
     */
    public static AnswerJson formatAnswerJson(String answerJsonContext){
        if(StringUtils.isNotEmpty(answerJsonContext)){
            Gson gson = new Gson();
            try {
                if(!answerJsonContext.toUpperCase().startsWith("%7B")){
                    answerJsonContext = URLEncoder.encode(answerJsonContext,"utf-8");
                }
                answerJsonContext = URLDecoder.decode(answerJsonContext,"utf-8");
                AnswerJson aj = gson.fromJson(answerJsonContext,AnswerJson.class);
                return aj;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 获取试题的答案内容
     * @param gson 格式工具
     * @param examQuestion 试题对象
     * @return 答案内容
     */
    public static String getQuestionAnswer(Gson gson,ExamQuestion examQuestion){
        TemplateContent templateContent = examQuestion.getTemplateContent();
        if(ObjectUtils.isNotEmpty(templateContent)){
            AnswerJson answerJson = new AnswerJson();
            answerJson.setTypeValue(templateContent.getType());
            if(templateContent.isHaveSubTrunk()){//有子试题
                answerJson.setType(2);
                List<TemplateContent> templateContentList = templateContent.getSubTrunkList();
                List<AnswerJson> subAnswerList = new ArrayList<>();
                templateContentList.forEach(e->{
                    AnswerJson subAnswerJson = new AnswerJson();
                    subAnswerJson.setType(1);
                    subAnswerJson.setTypeValue(e.getType());
                    subAnswerJson.setAnswerOptList(e.getAnswerOptList());
                    subAnswerJson.setAnswerBlankList(e.getAnswerBlankList());
                    subAnswerJson.setSpecialAnswer(e.getSpecialAnswer());
                    subAnswerList.add(subAnswerJson);
                });
                answerJson.setSubAnswerList(subAnswerList);
            }else{//无子试题
                answerJson.setType(1);
                answerJson.setAnswerOptList(templateContent.getAnswerOptList());
                answerJson.setAnswerBlankList(templateContent.getAnswerBlankList());
                answerJson.setSpecialAnswer(templateContent.getSpecialAnswer());
            }
            try {
                return URLEncoder.encode(gson.toJson(answerJson),"utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static String getUserAnswer(Gson gson,AnswerJson userAnswerJson){
        String strContext = gson.toJson(userAnswerJson);
        try {
            return URLEncoder.encode(strContext,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
